#!/bin/bash
# 测试脚本：run_benchmarks.sh
# 数据集路径：./lab2_generate_benchmark/
# 输出路径：./output/

# # 编译选项
# COMPILER="g++"
# FLAGS="-std=c++14 -O3"
TARGET="main"
# SRCS="main.cpp Arch.cpp Global.cpp Object.cpp Solution.cpp"

# # ---------- 初始化目录 ----------
echo "初始化目录结构..."
mkdir -p output logs

# # ---------- 编译程序 ----------
# echo "编译程序..."
# $COMPILER $FLAGS -o $TARGET $SRCS
# if [ $? -ne 0 ]; then
#     echo "编译失败！请检查代码错误"
#     exit 1
# fi

# ---------- 数据集配置 ----------
benchmarks=("small" "med1" "med2" "lg1" "lg2" "xl" "huge")

# ---------- 运行测试 ----------
echo "开始测试..."
echo "dataset,components,time(s),wirelength" > results.csv

for dataset in "${benchmarks[@]}"; do
    # 处理输入输出路径
    input_file="./lab2_generate_benchmark/${dataset}.txt"
    output_file="./output/${dataset}_placement.txt"
    log_file="./logs/${dataset}.log"
    
    # 检查输入文件是否存在
    if [ ! -f "$input_file" ]; then
        echo "[警告] 输入文件 $input_file 不存在，跳过该数据集"
        continue
    fi

    echo -n "正在测试 $dataset..."
    
    # 运行并计时
    start_time=$(date +%s.%N)
    ./$TARGET "$input_file" "$output_file" > "$log_file" 2>&1
    exit_code=$?
    end_time=$(date +%s.%N)
    runtime=$(printf "%.2f" $(echo "$end_time - $start_time" | bc))
    
    # 从日志提取关键信息
    wirelength=$(grep "Wirelength:" "$log_file" | awk '{print $NF}')
    
    
    # 记录结果
    echo "$dataset,$dataset,$runtime,$wirelength">> results.csv
    
    # 打印单次测试结果
    echo "完成! 用时: ${runtime}s | 线长: $wirelength "
done

# ---------- 生成报告 ----------
echo -e "\n测试结果汇总:"
column -t -s, results.csv | awk '
BEGIN {print "=================================================================="}
NR==1 {print; print "=================================================================="}
NR>1 {print}
END {print "=================================================================="}'

exit 0